﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ComicsEditor
{
    public partial class MetadataDialog : Form
    {
        private ComicInfo comicInfo;

        public MetadataDialog()
        {
            InitializeComponent();
        }

        public ComicInfo ComicInfo
        {
            set
            {
                comicInfo = value;

                foreach (KeyValuePair<string, string> entry in comicInfo.Metadata)
                {
                    int i = dataGridView1.Rows.Add();
                    SetRowValue(dataGridView1.Rows[i], entry.Key, entry.Value);
                }
            }
        }

        private void SetRowValue(DataGridViewRow row, string field, string value)
        {
            row.Cells[0].Value = field;

            if (field == "BlackAndWhite" || field == "Manga")
            {
                if (value == null)
                    value = "No";
                if (value == "Yes" || value == "No")
                {
                    DataGridViewCheckBoxCell cell = new DataGridViewCheckBoxCell();
                    cell.TrueValue = "Yes";
                    cell.FalseValue = "No";
                    row.Cells[1] = cell;
                }
            }
            if (field == "Count" || field == "Volume" || field == "AlternateCount" || field == "Year" || field == "Month")
            {
                int result;
                if (value == null || int.TryParse(value, out result))
                    row.Cells[1].ValueType = typeof(int);
            }
            if (field == "LanguageISO")
            {
                DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
                cell.DisplayMember = "Value";
                cell.ValueMember = "Key";
                #region LanguageISO
                cell.Items.Add(new KeyValuePair<string, string>("ab", "Abkhazian"));
                cell.Items.Add(new KeyValuePair<string, string>("aa", "Afar"));
                cell.Items.Add(new KeyValuePair<string, string>("af", "Afrikaans"));
                cell.Items.Add(new KeyValuePair<string, string>("sq", "Albanian"));
                cell.Items.Add(new KeyValuePair<string, string>("am", "Amharic"));
                cell.Items.Add(new KeyValuePair<string, string>("ar", "Arabic"));
                cell.Items.Add(new KeyValuePair<string, string>("hy", "Armenian"));
                cell.Items.Add(new KeyValuePair<string, string>("as", "Assamese"));
                cell.Items.Add(new KeyValuePair<string, string>("ay", "Aymara"));
                cell.Items.Add(new KeyValuePair<string, string>("az", "Azerbaijani"));
                cell.Items.Add(new KeyValuePair<string, string>("ba", "Bashkir"));
                cell.Items.Add(new KeyValuePair<string, string>("eu", "Basque"));
                cell.Items.Add(new KeyValuePair<string, string>("bn", "Bengali"));
                cell.Items.Add(new KeyValuePair<string, string>("dz", "Bhutani"));
                cell.Items.Add(new KeyValuePair<string, string>("bh", "Bihari"));
                cell.Items.Add(new KeyValuePair<string, string>("bi", "Bislama"));
                cell.Items.Add(new KeyValuePair<string, string>("br", "Breton"));
                cell.Items.Add(new KeyValuePair<string, string>("bg", "Bulgarian"));
                cell.Items.Add(new KeyValuePair<string, string>("my", "Burmese"));
                cell.Items.Add(new KeyValuePair<string, string>("be", "Byelorussian"));
                cell.Items.Add(new KeyValuePair<string, string>("km", "Cambodian"));
                cell.Items.Add(new KeyValuePair<string, string>("ca", "Catalan"));
                cell.Items.Add(new KeyValuePair<string, string>("zh", "Chinese"));
                cell.Items.Add(new KeyValuePair<string, string>("co", "Corsican"));
                cell.Items.Add(new KeyValuePair<string, string>("hr", "Croatian"));
                cell.Items.Add(new KeyValuePair<string, string>("cs", "Czech"));
                cell.Items.Add(new KeyValuePair<string, string>("da", "Danish"));
                cell.Items.Add(new KeyValuePair<string, string>("nl", "Dutch"));
                cell.Items.Add(new KeyValuePair<string, string>("en", "English"));
                cell.Items.Add(new KeyValuePair<string, string>("eo", "Esperanto"));
                cell.Items.Add(new KeyValuePair<string, string>("et", "Estonian"));
                cell.Items.Add(new KeyValuePair<string, string>("fo", "Faeroese"));
                cell.Items.Add(new KeyValuePair<string, string>("fa", "Farsi"));
                cell.Items.Add(new KeyValuePair<string, string>("fj", "Fiji"));
                cell.Items.Add(new KeyValuePair<string, string>("fi", "Finnish"));
                cell.Items.Add(new KeyValuePair<string, string>("fr", "French"));
                cell.Items.Add(new KeyValuePair<string, string>("fy", "Frisian"));
                cell.Items.Add(new KeyValuePair<string, string>("gl", "Galician"));
                cell.Items.Add(new KeyValuePair<string, string>("gd", "Gaelic (Scottish)"));
                cell.Items.Add(new KeyValuePair<string, string>("gv", "Gaelic (Manx)"));
                cell.Items.Add(new KeyValuePair<string, string>("ka", "Georgian"));
                cell.Items.Add(new KeyValuePair<string, string>("de", "German"));
                cell.Items.Add(new KeyValuePair<string, string>("el", "Greek"));
                cell.Items.Add(new KeyValuePair<string, string>("kl", "Greenlandic"));
                cell.Items.Add(new KeyValuePair<string, string>("gn", "Guarani"));
                cell.Items.Add(new KeyValuePair<string, string>("gu", "Gujarati"));
                cell.Items.Add(new KeyValuePair<string, string>("ha", "Hausa"));
                cell.Items.Add(new KeyValuePair<string, string>("he", "Hebrew"));
                cell.Items.Add(new KeyValuePair<string, string>("hi", "Hindi"));
                cell.Items.Add(new KeyValuePair<string, string>("hu", "Hungarian"));
                cell.Items.Add(new KeyValuePair<string, string>("is", "Icelandic"));
                cell.Items.Add(new KeyValuePair<string, string>("id", "Indonesian"));
                cell.Items.Add(new KeyValuePair<string, string>("ia", "Interlingua"));
                cell.Items.Add(new KeyValuePair<string, string>("ie", "Interlingue"));
                cell.Items.Add(new KeyValuePair<string, string>("iu", "Inuktitut"));
                cell.Items.Add(new KeyValuePair<string, string>("ik", "Inupiak"));
                cell.Items.Add(new KeyValuePair<string, string>("ga", "Irish"));
                cell.Items.Add(new KeyValuePair<string, string>("it", "Italian"));
                cell.Items.Add(new KeyValuePair<string, string>("ja", "Japanese"));
                cell.Items.Add(new KeyValuePair<string, string>("jv", "Javanese"));
                cell.Items.Add(new KeyValuePair<string, string>("kn", "Kannada"));
                cell.Items.Add(new KeyValuePair<string, string>("ks", "Kashmiri"));
                cell.Items.Add(new KeyValuePair<string, string>("kk", "Kazakh"));
                cell.Items.Add(new KeyValuePair<string, string>("rw", "Kinyarwanda"));
                cell.Items.Add(new KeyValuePair<string, string>("ky", "Kirghiz"));
                cell.Items.Add(new KeyValuePair<string, string>("rn", "Kirundi"));
                cell.Items.Add(new KeyValuePair<string, string>("ko", "Korean"));
                cell.Items.Add(new KeyValuePair<string, string>("ku", "Kurdish"));
                cell.Items.Add(new KeyValuePair<string, string>("lo", "Laothian"));
                cell.Items.Add(new KeyValuePair<string, string>("la", "Latin"));
                cell.Items.Add(new KeyValuePair<string, string>("lv", "Latvian"));
                cell.Items.Add(new KeyValuePair<string, string>("li", "Limburgish"));
                cell.Items.Add(new KeyValuePair<string, string>("ln", "Lingala"));
                cell.Items.Add(new KeyValuePair<string, string>("lt", "Lithuanian"));
                cell.Items.Add(new KeyValuePair<string, string>("mk", "Macedonian"));
                cell.Items.Add(new KeyValuePair<string, string>("mg", "Malagasy"));
                cell.Items.Add(new KeyValuePair<string, string>("ms", "Malay"));
                cell.Items.Add(new KeyValuePair<string, string>("ml", "Malayalam"));
                cell.Items.Add(new KeyValuePair<string, string>("mt", "Maltese"));
                cell.Items.Add(new KeyValuePair<string, string>("mi", "Maori"));
                cell.Items.Add(new KeyValuePair<string, string>("mr", "Marathi"));
                cell.Items.Add(new KeyValuePair<string, string>("mo", "Moldavian"));
                cell.Items.Add(new KeyValuePair<string, string>("mn", "Mongolian"));
                cell.Items.Add(new KeyValuePair<string, string>("na", "Nauru"));
                cell.Items.Add(new KeyValuePair<string, string>("ne", "Nepali"));
                cell.Items.Add(new KeyValuePair<string, string>("no", "Norwegian"));
                cell.Items.Add(new KeyValuePair<string, string>("oc", "Occitan"));
                cell.Items.Add(new KeyValuePair<string, string>("or", "Oriya"));
                cell.Items.Add(new KeyValuePair<string, string>("om", "Oromo"));
                cell.Items.Add(new KeyValuePair<string, string>("ps", "Pashto"));
                cell.Items.Add(new KeyValuePair<string, string>("pl", "Polish"));
                cell.Items.Add(new KeyValuePair<string, string>("pt", "Portuguese"));
                cell.Items.Add(new KeyValuePair<string, string>("pa", "Punjabi"));
                cell.Items.Add(new KeyValuePair<string, string>("qu", "Quechua"));
                cell.Items.Add(new KeyValuePair<string, string>("rm", "Rhaeto-Romance"));
                cell.Items.Add(new KeyValuePair<string, string>("ro", "Romanian"));
                cell.Items.Add(new KeyValuePair<string, string>("ru", "Russian"));
                cell.Items.Add(new KeyValuePair<string, string>("sm", "Samoan"));
                cell.Items.Add(new KeyValuePair<string, string>("sg", "Sangro"));
                cell.Items.Add(new KeyValuePair<string, string>("sa", "Sanskrit"));
                cell.Items.Add(new KeyValuePair<string, string>("sr", "Serbian"));
                cell.Items.Add(new KeyValuePair<string, string>("sh", "Serbo-Croatian"));
                cell.Items.Add(new KeyValuePair<string, string>("st", "Sesotho"));
                cell.Items.Add(new KeyValuePair<string, string>("tn", "Setswana"));
                cell.Items.Add(new KeyValuePair<string, string>("sn", "Shona"));
                cell.Items.Add(new KeyValuePair<string, string>("sd", "Sindhi"));
                cell.Items.Add(new KeyValuePair<string, string>("si", "Sinhalese"));
                cell.Items.Add(new KeyValuePair<string, string>("ss", "Siswati"));
                cell.Items.Add(new KeyValuePair<string, string>("sk", "Slovak"));
                cell.Items.Add(new KeyValuePair<string, string>("sl", "Slovenian"));
                cell.Items.Add(new KeyValuePair<string, string>("so", "Somali"));
                cell.Items.Add(new KeyValuePair<string, string>("es", "Spanish"));
                cell.Items.Add(new KeyValuePair<string, string>("su", "Sundanese"));
                cell.Items.Add(new KeyValuePair<string, string>("sw", "Swahili"));
                cell.Items.Add(new KeyValuePair<string, string>("sv", "Swedish"));
                cell.Items.Add(new KeyValuePair<string, string>("tl", "Tagalog"));
                cell.Items.Add(new KeyValuePair<string, string>("tg", "Tajik"));
                cell.Items.Add(new KeyValuePair<string, string>("ta", "Tamil"));
                cell.Items.Add(new KeyValuePair<string, string>("tt", "Tatar"));
                cell.Items.Add(new KeyValuePair<string, string>("te", "Telugu"));
                cell.Items.Add(new KeyValuePair<string, string>("th", "Thai"));
                cell.Items.Add(new KeyValuePair<string, string>("bo", "Tibetan"));
                cell.Items.Add(new KeyValuePair<string, string>("ti", "Tigrinya"));
                cell.Items.Add(new KeyValuePair<string, string>("to", "Tonga"));
                cell.Items.Add(new KeyValuePair<string, string>("ts", "Tsonga"));
                cell.Items.Add(new KeyValuePair<string, string>("tr", "Turkish"));
                cell.Items.Add(new KeyValuePair<string, string>("tk", "Turkmen"));
                cell.Items.Add(new KeyValuePair<string, string>("tw", "Twi"));
                cell.Items.Add(new KeyValuePair<string, string>("ug", "Uighur"));
                cell.Items.Add(new KeyValuePair<string, string>("uk", "Ukrainian"));
                cell.Items.Add(new KeyValuePair<string, string>("ur", "Urdu"));
                cell.Items.Add(new KeyValuePair<string, string>("uz", "Uzbek"));
                cell.Items.Add(new KeyValuePair<string, string>("vi", "Vietnamese"));
                cell.Items.Add(new KeyValuePair<string, string>("vo", "Volapük"));
                cell.Items.Add(new KeyValuePair<string, string>("cy", "Welsh"));
                cell.Items.Add(new KeyValuePair<string, string>("wo", "Wolof"));
                cell.Items.Add(new KeyValuePair<string, string>("xh", "Xhosa"));
                cell.Items.Add(new KeyValuePair<string, string>("yi", "Yiddish"));
                cell.Items.Add(new KeyValuePair<string, string>("yo", "Yoruba"));
                cell.Items.Add(new KeyValuePair<string, string>("zu", "Zulu"));
                #endregion
                bool found = value == null;
                foreach (KeyValuePair<string, string> pair in cell.Items)
                    if (pair.Key == value)
                        found = true;
                if (!found)
                    cell.Items.Insert(0, new KeyValuePair<string, string>(value, value));
                row.Cells[1] = cell;
            }
            if (field == "Type")
            {
                DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
                cell.DisplayMember = "Value";
                cell.ValueMember = "Key";
                cell.Items.Add("Unknown");
                cell.Items.Add("ComicBook");
                cell.Items.Add("ComicStrip");
                cell.Items.Add("WebComic");
                cell.Items.Add("GraphicNovel");
                cell.Items.Add("Manga");
                cell.Items.Add("Yonkoma");
                cell.Items.Add("Lubok");
                bool found = value == null;
                foreach (string type in cell.Items)
                    if (type == value)
                        found = true;
                if (!found)
                    cell.Items.Insert(0, value);
                row.Cells[1] = cell;
            }

            row.Cells[1].Value = value;
        }


        private void buttonOK_Click(object sender, EventArgs e)
        {
            comicInfo.Metadata.Clear();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string field = (string)row.Cells[0].Value;
                string value = (row.Cells[1].Value ?? "").ToString();
                if (value != "")
                    comicInfo.Metadata.Add(new KeyValuePair<string, string>(field, value));
            }
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            foreach (string field in ComicInfo.MetadataFields)
            {
                bool found = false;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                    if ((string)row.Cells[0].Value == field)
                        found = true;
                if (!found)
                    comboBox1.Items.Add(field);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                int i = dataGridView1.Rows.Add();
                SetRowValue(dataGridView1.Rows[i], (string)comboBox1.SelectedItem, null);
                dataGridView1.CurrentCell = dataGridView1[1, i];
                dataGridView1.BeginEdit(false);
            }
        }
    }
}
